package com.lzx.reader;

import com.csvreader.CsvReader;
import com.monitorjbl.xlsx.StreamingReader;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;


import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: 如寄
 * @version: v1.0
 * @description: com.lzx.reader
 * @date:2021/8/5
 */
public class DataReader {

    public static Map<String, List<List<String>>> readCSV(InputStream inputStream) throws FileNotFoundException {
        Map<String, List<List<String>>> data = new HashMap<>(10000);
        try {
            // 创建CSV读对象
            CsvReader csvReader = new CsvReader(inputStream, ',', Charset.forName("GBK"));
            // 读表头
            csvReader.readHeaders();
            while (csvReader.readRecord()) {
                // 读一整行
                String title = csvReader.get(0);
                String content = csvReader.get(1);
                String channel = csvReader.get(2);
                List<String> list = new ArrayList<>();
                list.add(title);
                list.add(content);
                if (data.containsKey(channel)) {
                    data.get(channel).add(list);
                } else {
                    List<List<String>> temp = new ArrayList<>();
                    temp.add(list);
                    data.put(channel, temp);
                }
            }
            csvReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return data;
    }
}
